﻿using Cognex.VisionPro;
using Cognex.VisionPro.Display;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.Implementation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YJYpublicClass;
using YJYpublicClass.DataStruct;

namespace 焱信引导检测.CommonClass
{
    internal class VisionpComFuc
    {
        public static ResultFunc Saveimage(string filepath,string errstr, bool bisOK, Cognex.VisionPro.CogRecordDisplay RecorDis)
        {
            try
            {
                string imagefile = "";

                if (bisOK)
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "OK\\";
                }
                else
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "NG\\";
                }

                imagefile = imagefile + DateTime.Now.ToString("HHmmss") + "_" + errstr + ".bmp";
                if (!File.Exists(imagefile))
                {
                    XmlHelper.CreateDirectoryEx(imagefile);
                }
                Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();
                if (RecorDis.Image != null)
                {
                    saveima.InputImage = RecorDis.Image;
                    saveima.Operator.Open(imagefile, CogImageFileModeConstants.Write);
                    saveima.Run();
                    return new ResultFunc();
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "图片保存失败:图片为空");
                }
                
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }
      
        public static ResultFunc Saveimage(string filepath, string weistr, bool bisOK, ICogImage saveImage)
        {
            try
            {
                string imagefile = "";

                if (bisOK)
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "OK\\";
                }
                else
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "NG\\";
                }

                imagefile = imagefile + DateTime.Now.ToString("HHmmss") + "_" + weistr + ".bmp";
                if (!File.Exists(imagefile))
                {
                    XmlHelper.CreateDirectoryEx(imagefile);
                }
                Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();
                if (saveImage != null)
                {
                    saveima.InputImage = saveImage;
                    saveima.Operator.Open(imagefile, CogImageFileModeConstants.Write);
                    saveima.Run();
                    return new ResultFunc();
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "图片保存失败:图片为空");
                }

            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }


        public static ResultFunc SaveimageDisp(string filepath, int type, string errstr, bool bisOK, Cognex.VisionPro.CogRecordDisplay RecorDis)
        {
            try
            {
                string imagefile = "";

                if (bisOK)
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "OK\\";
                }
                else
                {
                    imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + "NG\\";
                }

                imagefile = imagefile + DateTime.Now.ToString("HHmmss") + "_" + errstr + ".bmp";
                if (!File.Exists(imagefile))
                {
                    XmlHelper.CreateDirectoryEx(imagefile);
                }
                Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();
                if (RecorDis.Image != null)
                {
                    saveima.InputImage = RecorDis.Image;
                    saveima.Operator.Open(imagefile, CogImageFileModeConstants.Write);
                    saveima.Run();
                    return new ResultFunc();
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "图片保存失败:图片为空");
                }
              
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }

        public static ResultFunc SaveimageDisp(string filepath, string filebisOK, string houzui, Cognex.VisionPro.CogRecordDisplay RecorDis)
        {
            try
            {
                string imagefile = "";         
                imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + filebisOK+ "\\";
                imagefile = imagefile + DateTime.Now.ToString("HHmmss") +"_"+ houzui+".bmp";
                if (!File.Exists(imagefile))
                {
                    XmlHelper.CreateDirectoryEx(imagefile);
                }
                Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();
                if (RecorDis.Image != null)
                {
                    //saveima.InputImage = RecorDis.
                    saveima.InputImage = RecorDis.Image;
                    saveima.Operator.Open(imagefile, CogImageFileModeConstants.Write);
                    saveima.Run();
                    return new ResultFunc();
                   
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "图片保存失败:图片为空");
                }
             
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }

        public static ResultFunc SaveimageDispjietu(string filepath, string filebisOK, string houzui, Cognex.VisionPro.CogRecordDisplay RecorDis)
        {
            try
            {
                string imagefile = "";  
                imagefile = filepath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + filebisOK+ "\\";
                imagefile = imagefile + DateTime.Now.ToString("HHmmss") +"_"+ houzui+".jpg";
                if (!File.Exists(imagefile))
                {
                    XmlHelper.CreateDirectoryEx(imagefile);
                }
                //   Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();
                if (RecorDis.Image != null)
                {
                    //saveima.InputImage = RecorDis.
                    Bitmap saveimagec = RecorDis.CreateContentBitmap(CogDisplayContentBitmapConstants.Image) as Bitmap;
                    saveimagec.Save(imagefile, ImageFormat.Jpeg);
                    return new ResultFunc();
                }
                else
                {
                
                    return new ResultFunc(Funstate.异常, "图片保存失败:图片为空");
                }
                
            }
            catch (Exception ex)
            {
             
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }

        /// <summary>
        /// 固定间隔删除文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Interval"></param>
        public static ResultFunc DeleteFile(string FilePath, int Interval)
        {
            try
            {
                DirectoryInfo rootDirInfo = new DirectoryInfo(FilePath);
                DateTime NowTime = DateTime.Now;
                FileSystemInfo[] dirs = rootDirInfo.GetFileSystemInfos();

                foreach (var item in dirs)
                {
                    DateTime CreateFileTime = item.CreationTime;
                    TimeSpan OutTime = NowTime - CreateFileTime;
                    if (OutTime.Days > Interval)
                    {
                        FileAttributes Attr = File.GetAttributes(item.FullName);
                        if (Attr == FileAttributes.Directory) { Directory.Delete(item.FullName, true); }
                        else { item.Delete(); }
                    }
                }
                return new ResultFunc();
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "删除文件失败" + ex.ToString());
               // ColoredOutput.WriteLine(PresetColor.Red, "删除文件失败" + ex.ToString());
            }
        }
        public static void drawrectangle(Cognex.VisionPro.CogRecordDisplay RecorDis, double x1, double y1, double width, double height, CogColorConstants color)
        {
            CogRectangle rect1 = new CogRectangle();
            rect1.X = x1;
            rect1.Y = y1;
            rect1.Width = width;
            rect1.Height = height;
            rect1.Color = color;
            RecorDis.StaticGraphics.Add(rect1, "a");
        }
        public static void draw_text(Cognex.VisionPro.CogRecordDisplay RecorDis, double X, double Y, double Rotation, string text, CogColorConstants color1, int size)
        {
            CogGraphicLabel str = new CogGraphicLabel();
            str.SetXYText(X, Y, text);
            str.Rotation = Rotation;
            str.Color = color1;
            str.Font = new System.Drawing.Font("宋体", size, FontStyle.Regular);
            RecorDis.StaticGraphics.Add(str, "b");
        }
        public static void draw_point(Cognex.VisionPro.CogRecordDisplay RecorDis, double X, double Y, CogColorConstants color1)
        {
            CogPointMarker point = new CogPointMarker();
            point.X = X;
            point.Y = Y;
            point.Color= color1;

            RecorDis.StaticGraphics.Add(point, "a");
        }
        public static void draw_circle(Cognex.VisionPro.CogRecordDisplay RecorDis, double X, double Y, double r, CogColorConstants color1)
        {
            try
            {
                CogCircle point = new CogCircle();
                point.CenterX = X;
                point.CenterY = Y;
                point.Color = color1;
                point.Radius = r;

                RecorDis.StaticGraphics.Add(point, "a");
            }
            catch (Exception ex)
            {

            }

        }
        public static CogRecord CreateDisplayRecord(Cognex.VisionPro.ICogImage image)
        {        
            return new CogRecord("InputImage",
                typeof(Cognex.VisionPro.CogImage8Grey),
                Cognex.VisionPro.CogRecordUsageConstants.Input, false, image, "InputImage");
        
        }
        public static ResultFunc Loadimage(string filepath,out ICogImage outImage)
        {
            outImage = null;
            try
            {              
                Cognex.VisionPro.ImageFile.CogImageFileTool saveima = new Cognex.VisionPro.ImageFile.CogImageFileTool();    
                saveima.Operator.Open(filepath, CogImageFileModeConstants.Read);
                saveima.Run();
                outImage =  saveima.OutputImage;
                return new ResultFunc();
            }
            catch (Exception ex)
            {
                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }
        public static ResultFunc Dispimage(Cognex.VisionPro.CogRecordDisplay DispWindow,  ICogImage outImage)
        {
            try
            {
               
                DispWindow.Record =CreateDisplayRecord(outImage);
                DispWindow.Fit();
                return new ResultFunc();
            }
            catch (Exception ex)
            {

                return new ResultFunc(Funstate.异常, "图片保存失败:"  + ex.Message);
            }
        }

    }
}
